<?php /* Template Name: Galleries */ ?>

<?php get_header(); ?>
    
<?php 

    //VARS
    $default_excerpt_length = 400;

    $query_args = array(
        'numberposts'       => -1,
        'post_type'         => 'page',
        'post_status'       => 'publish',
        'orderby'           => 'post_date',
        'order'             => 'DESC',
        'meta_key'          => '_wp_page_template',
        'meta_value'        => 'page-gallery.php'
    );


    //GET GALLERIES
    $results_query = get_posts($query_args); 
    $gallery_counter = 0;

?>
    	
    	
    <!-- START MAIN-CONTAINER -->
    <div class="main-container">
        <!-- START MAIN WRAPPER -->
        <div class="main wrapper clearfix">
            <!-- START MAIN-CONTENT -->
            <div class="main-content full">

        
    	                	
                        <!-- REPEAT FOR EACH GALLERY -->

                        <?php 
                            foreach ($results_query as $gallery) {

                                $gallery_counter++;

                                //SETTTINGS
                                $result_cmb_gallery_style = get_post_meta($gallery->ID, 'cmb_gallery_style', true);
                                $result_cmb_gallery_click = get_post_meta($gallery->ID, 'cmb_gallery_click', true);
                                $result_cmb_gallery_excerpt = get_post_meta($gallery->ID, 'cmb_gallery_excerpt', true);
                                $result_cmb_gallery_cat_ID = get_post_meta($gallery->ID, 'cmb_gallery_cat_ID', true);
                                $result_cmb_gallery_client_name = get_post_meta($gallery->ID, 'cmb_gallery_client_name', true);
                                $result_cmb_gallery_client_url = get_post_meta($gallery->ID, 'cmb_gallery_client_url', true);

                                $post_counter = 1;

                                //NUMBERPOSTS
                                $numposts = -1;
                                switch ($result_cmb_gallery_style) {
                                    case "one":
                                        $numposts = -1;
                                        break;
                                    case "two":
                                        $numposts = 9;
                                        break;
                                    case "three":
                                        $numposts = 1;
                                        break;
                                    default:
                                        $numposts = -1;

                                }

                                //BUILD EXCLUDE ARRAY
                                $results_exclude_posts = get_posts(array(
                                    'numberposts'       => -1,
                                    'meta_key'          => 'cmb_hide_from_gallery',
                                    'meta_value'        => 'checked',
                                    'orderby'           => 'post_date',
                                    'order'             => 'DESC',
                                    'post_type'         => 'any',
                                    'suppress_filters'  => true,
                                ));
                                if (count($results_exclude_posts) > 0) {
                                    for ($i = 0; $i < count($results_exclude_posts); $i++) {  
                                        $exclude_array[$i] = $results_exclude_posts[$i]->ID;
                                    }   
                                } else {
                                    $exclude_array = array();   
                                }

                                //BUILD INCLUDE STRING
                                $include_string = " ";  //notice the extra space - to prevent include string from being empty which would display all categories.

                                if (!empty($result_cmb_gallery_cat_ID)) {
                                    foreach ($result_cmb_gallery_cat_ID as $key => $value) {
                                        if ($value == "checked") $include_string .=  $key . ",";
                                    }
                                    $include_string = substr($include_string,0,strlen($include_string)-1);
                                } 

                                //BASE ARGS
                                $query_args_posts = array(
                                    'numberposts'       => -1,
                                    'post_type'         => 'post',
                                    'post_status'       => 'publish',
                                    'orderby'           => 'post_date',
                                    'order'             => 'DESC',
                                    'post__not_in'      => $exclude_array,
                                    'cat'               => $include_string
                                );

                                //GET POSTS IN GALLERY
                                $results_posts = get_posts($query_args_posts);

                            ?>


                            <?php 

                                //GALLERY STYLE 1
                                if ($result_cmb_gallery_style == "one") {
                                ?>

                                    <!-- start gallery style 1--> 
                                    <div class="clearfix">
                                       
                                        <div class="third">
                                            
                                            <h2><?php echo $gallery->post_title; ?></h2>

                                            <p><?php if (empty($result_cmb_gallery_excerpt)) { echo mb_make_excerpt($gallery->post_content, $default_excerpt_length, true); } else {echo do_shortcode($result_cmb_gallery_excerpt);} ?></p>
                                            <p><a href="<?php echo get_permalink($gallery->ID); ?>" class="btn"><?php _e("See More", "loc_canon"); ?></a></p>    
                                            
                                        </div>

                                        <!-- gallery images -->
                                            <div class="two-thirds last">
                                            <!-- Start Slider -->
                                            <div class="flexslider">
                                                <ul class="slides">

                                                    <!-- REPEAT FOR EACH POST IN GALLERY -->
                                                    <?php 
                                                        foreach($results_posts as $current_post) {

                                                            $result_cmb_media_link = get_post_meta($current_post->ID, 'cmb_media_link', true);
                                                            $result_cmb_use_media_link = get_post_meta($current_post->ID, 'cmb_use_media_link', true);

                                                            if (!empty($result_cmb_use_media_link) && !empty($result_cmb_media_link)) {
                                                                echo "<li>";
                                                                echo $result_cmb_media_link;        
                                                                echo "</li>";
                                                                $post_counter++;
                                                            } elseif (has_post_thumbnail($current_post->ID) && get_post(get_post_thumbnail_id($current_post->ID))) {
                                                                $post_thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($current_post->ID),'full');
                                                                $img_alt = get_post_meta(get_post_thumbnail_id($current_post->ID), '_wp_attachment_image_alt', true);
                                                                $img_post = get_post(get_post_thumbnail_id($current_post->ID));
                                                                if ($result_cmb_gallery_click == "post") {
                                                                    printf("<li><a href='%s'><img src='%s' alt='%s'></a></li>", get_permalink($current_post->ID), esc_url($post_thumbnail_src[0]), esc_attr($img_alt)); 
                                                                } else {
                                                                    printf("<li><a href='%s'><img src='%s' alt='%s'></a></li>", esc_url($post_thumbnail_src[0]), esc_url($post_thumbnail_src[0]), esc_attr($img_alt)); 
                                                                }
                                                                $post_counter++;
                                                            }

                                                        }

                                                    ?>


                                            </div>
                                        </div>
                                    
                                    </div>
                                    <!-- end gallery style 1--> 

                                <?php
                                }


                                //GALLERY STYLE 2
                                if ($result_cmb_gallery_style == "two") {
                                ?>

                                    <!-- start gallery style 2--> 
                                    <div class="clearfix">

                                        <div class="third">
                                            <h2><?php echo $gallery->post_title; ?></h2>

                                            <p><?php if (empty($result_cmb_gallery_excerpt)) { echo mb_make_excerpt($gallery->post_content, $default_excerpt_length, true); } else {echo do_shortcode($result_cmb_gallery_excerpt);} ?></p>
                                            <p><a href="<?php echo get_permalink($gallery->ID); ?>" class="btn"><?php _e("See More", "loc_canon"); ?></a></p>    
                                        </div>

                                        <div class="two-thirds right last thumb-gallery">

                                            <!-- REPEAT FOR EACH POST IN GALLERY -->
                                            <?php 
                                                foreach($results_posts as $current_post) {

                                                    if ($post_counter < $numposts+1) {

                                                        $result_cmb_media_link = get_post_meta($current_post->ID, 'cmb_media_link', true);
                                                        $result_cmb_use_media_link = get_post_meta($current_post->ID, 'cmb_use_media_link', true);
                                                       
                                                        $last_class = ($post_counter%3) ? "" : " last";
                                                        $item_categories = get_the_terms($current_post->ID, 'category');
                                                        if ($item_categories) foreach ($item_categories as $value) $last_class .= " cat-item-" . $value->term_id;


                                                        if (!empty($result_cmb_use_media_link) && !empty($result_cmb_media_link)) {
                                                            echo '<div class="gallery_item third'.$last_class.'">';
                                                            echo $result_cmb_media_link;        
                                                            echo '</div>';
                                                            $post_counter++;
                                                        } elseif (has_post_thumbnail($current_post->ID) && get_post(get_post_thumbnail_id($current_post->ID)) ) {
                                                            echo '<div class="gallery_item mosaic-block third fade element'.$last_class.'">';
                                                            $post_thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($current_post->ID),'full');
                                                            $post_thumbnail_src_fit = wp_get_attachment_image_src(get_post_thumbnail_id($current_post->ID),'gallery_isotope_x2');
                                                            $img_alt = get_post_meta(get_post_thumbnail_id($current_post->ID), '_wp_attachment_image_alt', true);
                                                            $img_post = get_post(get_post_thumbnail_id($current_post->ID));
                                                            if ($result_cmb_gallery_click == "post") {
                                                                printf('<a href="%s" class="mosaic-overlay link fancybox" title="%s"></a>', get_permalink($current_post->ID), esc_attr($img_post->post_title));
                                                            } else {
                                                                printf('<a href="%s" class="mosaic-overlay fancybox" title="%s"></a>', esc_url($post_thumbnail_src[0]), esc_attr($img_post->post_title));
                                                            }
                                                            printf('<div class="mosaic-backdrop"><img src="%s" alt="%s" /></div>', esc_url($post_thumbnail_src_fit[0]), esc_attr($img_alt));
                                                            echo '</div>';
                                                            $post_counter++;
                                                        }
                                                    }

                                                } //end foreach

                                            ?>

                                        </div>

                                    </div>                              
                                    <!-- end gallery style 2--> 
                                    
                                <?php
                                }

                                //GALLERY STYLE 3
                                if ($result_cmb_gallery_style == "three") {

                                ?>

                                    <div class="clearfix">

                                        <div class="third">
                                            <h2><?php echo $gallery->post_title; ?></h2>

                                            <p><?php if (empty($result_cmb_gallery_excerpt)) { echo mb_make_excerpt($gallery->post_content, $default_excerpt_length, true); } else {echo do_shortcode($result_cmb_gallery_excerpt);} ?></p>
                                            <p><a href="<?php echo get_permalink($gallery->ID); ?>" class="btn"><?php _e("See More", "loc_canon"); ?></a></p>    
                                        </div>

                                        <div class="two-thirds right last">

                                            <!-- REPEAT FOR EACH POST IN GALLERY -->
                                            <?php 
                                                foreach($results_posts as $current_post) {

                                                    if ($post_counter < $numposts+1) {
                                                        $result_cmb_media_link = get_post_meta($current_post->ID, 'cmb_media_link', true);
                                                        $result_cmb_use_media_link = get_post_meta($current_post->ID, 'cmb_use_media_link', true);
                                                       

                                                        if (!empty($result_cmb_use_media_link) && !empty($result_cmb_media_link)) {
                                                            echo $result_cmb_media_link;        
                                                            $post_counter++;
                                                        } elseif (has_post_thumbnail($current_post->ID) && get_post(get_post_thumbnail_id($current_post->ID)) ) {
                                                            echo '<div class="mosaic-block fade">';
                                                            $post_thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($current_post->ID),'full');
                                                            $post_thumbnail_src_fit = wp_get_attachment_image_src(get_post_thumbnail_id($current_post->ID),'gallery_isotope_x2');
                                                            $img_alt = get_post_meta(get_post_thumbnail_id($current_post->ID), '_wp_attachment_image_alt', true);
                                                            $img_post = get_post(get_post_thumbnail_id($current_post->ID));
                                                            if ($result_cmb_gallery_click == "post") {
                                                                printf('<a href="%s" class="mosaic-overlay link fancybox" title="%s"></a>', get_permalink($current_post->ID), esc_attr($img_post->post_title));
                                                            } else {
                                                                printf('<a href="%s" class="mosaic-overlay fancybox" title="%s"></a>', esc_url($post_thumbnail_src[0]), esc_attr($img_post->post_title));
                                                            }
                                                            printf('<div class="mosaic-backdrop"><img src="%s" alt="%s" /></div>', esc_url($post_thumbnail_src[0]), esc_attr($img_alt));
                                                            echo '</div>';
                                                            $post_counter++;
                                                        }
                                                    }

                                                } //end foreach

                                            ?>

                                        </div>

                                    </div>                              
                                    <!-- end gallery style 3--> 
                                    
                                <?php
                                }


                            ?>


                            <?php 
                                //ADD HR TO END OF GALLERIES UNLESS LAST
                                if ($gallery_counter < count($results_query)) {
                                    echo "<hr>";
                                }
                            ?>

                            <?php
                            }
                            //END FOREACH GALLERY

                        ?>
                                                                       
            </div>
            <!-- END MAIN-CONTENT -->
        </div> 
        <!-- END MAIN WRAPPER -->
    </div> 
    <!-- END MAIN-CONTAINER -->
 
        
    	
    	
    	
<?php get_footer(); ?>